package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.StringUtils;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.wb.dao.WbPjTargetDao;
import com.cyt.work.wb.model.po.WbPjTarget;
import com.cyt.work.wb.model.vo.WbPjTargetVo;
import com.cyt.work.wb.service.WbPjTargetService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 评价指标-wbPjTargetService层实现类
 *
 * @author dingxu
 * @date 2019-12-25 9:32:19
 */
@Service("wbPjTargetService")
public class WbPjTargetServiceImpl extends BaseServiceImpl<WbPjTarget> implements WbPjTargetService {

    @Resource
    private WbPjTargetDao wbPjTargetDao;

    @Autowired
    public void setBaseDao(WbPjTargetDao wbPjTargetDao) {
        this.baseDao = wbPjTargetDao;
    }

    /**
     * 查询对象集合
     *
     * @param wbPjTargetVo 对象
     * @return List 对象集合
     * @author dingxu
     * @date 2019-12-25 9:32:19
     */
    @Override
    public List<WbPjTarget> listExample(WbPjTargetVo wbPjTargetVo) {
        Example example = new Example(WbPjTarget.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (StringUtils.isNotBlank(wbPjTargetVo.getTitle())) {
            criteria.andLike("title", "%" + wbPjTargetVo.getTitle() + "%");
        }
        if (StringUtils.isNotBlank(wbPjTargetVo.getType())) {
            criteria.andEqualTo("type", wbPjTargetVo.getType());
        }
        return wbPjTargetDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param wbPjTargetVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-25 9:32:19
     */
    @Override
    public ReturnResult page(WbPjTargetVo wbPjTargetVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbPjTarget> list = listExample(wbPjTargetVo);
        PageInfo<WbPjTarget> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param wbPjTargetVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-25 9:32:19
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbPjTargetVo wbPjTargetVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbPjTargetVo.getId())) {
            WbPjTarget wbPjTarget = new WbPjTarget(sysUser);
            wbPjTarget.setTitle(wbPjTargetVo.getTitle());
            wbPjTarget.setType(wbPjTargetVo.getType());
            wbPjTarget.setValidState(wbPjTargetVo.getValidState());
            wbPjTargetDao.insert(wbPjTarget);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbPjTarget wbPjTarget = wbPjTargetDao.selectByPrimaryKey(wbPjTargetVo.getId());
            wbPjTarget.setTitle(wbPjTargetVo.getTitle());
            wbPjTarget.setType(wbPjTargetVo.getType());
            wbPjTarget.setValidState(wbPjTargetVo.getValidState());
            wbPjTarget.setUpdateTime(new Date());
            wbPjTarget.setUpdateUser(sysUser.getId());
            wbPjTargetDao.updateByPrimaryKey(wbPjTarget);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbPjTargetVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-25 9:32:19
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbPjTargetVo wbPjTargetVo, SysUser sysUser) {
        //要修改的字段
        WbPjTarget wbPjTarget = new WbPjTarget();
        wbPjTarget.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbPjTarget.setUpdateUser(sysUser.getId());
        wbPjTarget.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbPjTarget.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbPjTargetVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbPjTargetVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbPjTargetDao.updateByExampleSelective(wbPjTarget, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbPjTargetVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author dingxu
     * @date 2019-12-25 9:32:19
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbPjTargetVo wbPjTargetVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbPjTarget.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbPjTargetVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbPjTargetVo.getId().split(",")));
        }
        wbPjTargetDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}